﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Samples.ServiceHosting.StorageClient;
using System.Collections.Specialized;
using System.IO;
using System.Configuration;

namespace HotSpot.Util
{
    /// <summary>
    /// Managing documents in HotSpot.    
    /// </summary>
    public static class HotSpotDocument
    {
        #region Internal static members
        static readonly string azureBlobStorageAccountName =            
            ConfigurationManager.AppSettings["AzureBlobStorageAccountName"].ToString();
        static readonly string azureBlobStorageSharedKey =
            ConfigurationManager.AppSettings["AzureBlobStorageSharedKey"].ToString();
        static readonly string azureBlobStorageEndpoint =
            ConfigurationManager.AppSettings["AzureBlobStorageEndpoint"].ToString();
        static readonly string azureBlobStorageContainer =
            ConfigurationManager.AppSettings["AzureBlobStorageContainer"].ToString();
        #endregion

        #region Blog managing
        /// <summary>
        /// Upload file to "Documents" container on HotSpotStorage storage account
        /// </summary>
        /// <param name="fileName">Name of the file to upload</param>
        public static void Upload(string fileName, byte[] fileData)
        {
            //An URI
            Uri uri = new Uri(azureBlobStorageEndpoint);

            //Information about the storage account
            StorageAccountInfo storageAccountInfo = new StorageAccountInfo(
                uri,
                null,
                azureBlobStorageAccountName,
                azureBlobStorageSharedKey);

            //Create the BlobStorage object
            BlobStorage blobStorage = BlobStorage.Create(storageAccountInfo);

            //If container does not exist, create a new one
            BlobContainer blobContainer = blobStorage.GetBlobContainer(azureBlobStorageContainer);
            if (!blobContainer.DoesContainerExist())
                blobContainer.CreateContainer(
                    new NameValueCollection(), ContainerAccessControl.Public);

            //Saving information about the file
            NameValueCollection nameValueCollection = new NameValueCollection();
            nameValueCollection["FileName"] = fileName;

            BlobProperties blobProperties = new BlobProperties(fileName);
            blobProperties.Metadata = nameValueCollection;

            BlobContents blobContents = new BlobContents(fileData);
            blobContainer.CreateBlob(blobProperties, blobContents, true);
        }

        /// <summary>
        /// Download file from "Documents" container on HotSpotStorage storage account
        /// </summary>
        /// <param name="fileName">Name of the file to download</param>
        /// <returns>File data</returns>
        public static byte[] Download(string fileName)
        {
            //An URI
            Uri uri = new Uri(azureBlobStorageEndpoint);

            //Information about the storage account
            StorageAccountInfo storageAccountInfo = new StorageAccountInfo(
                uri,
                null,
                azureBlobStorageAccountName,
                azureBlobStorageSharedKey);

            //Create the BlobStorage object
            BlobStorage blobStorage = BlobStorage.Create(storageAccountInfo);

            //If container does not exist, create a new one
            BlobContainer blobContainer = blobStorage.GetBlobContainer(azureBlobStorageContainer);
            
            BlobContents blobContents = new BlobContents(new MemoryStream());
            BlobProperties blobProperties = blobContainer.GetBlob(fileName, blobContents, true);

            return blobContents.AsBytes();
        }
        #endregion
    }
}
